<?php


namespace app\admin\controller;


use app\admin\model\CommunityModel;
use app\admin\model\CompanyModel;
use app\admin\model\RoleUserModel;
use app\admin\model\StoreModel;
use app\admin\model\UserModel;
use app\admin\model\UserRoleModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;
use think\facade\Env;

class ShareController extends AdminBaseController
{
    public function index()
    {
        $admin_id = cmf_get_current_admin_id();
        $role_id = RoleUserModel::where('user_id', $admin_id)->value('role_id');
        $role_id = $role_id ? $role_id : 1;
        $this->assign('role_id', $role_id);
        $admin_id = cmf_get_current_admin_id();
        $admin_user_info = UserModel::get($admin_id);
        if ($role_id == 3) {
            $company_id = $admin_user_info->company_id;
        } else {
            $company_id = '';
        }

        if ($this->request->isAjax()) {
            $data = $this->request->param();

            if (isset($data['user_card_register'])) {
                Db::name('option')->where('option_name','user_card_register'.$company_id)->update(['option_value'=>$data['user_card_register']]);
            }

            if (isset($data['user_buy'])) {
                Db::name('option')->where('option_name','user_buy'.$company_id)->update(['option_value'=>$data['user_buy']]);
            }

            if (isset($data['sharing_of_rewards'])) {
                Db::name('option')->where('option_name','sharing_of_rewards'.$company_id)->update(['option_value'=>$data['sharing_of_rewards']]);
            }

            if ($role_id == 3) {
                $list = [];
            } else {
                $where = [];
                $page = 1;
                if (isset($data['page'])) {
                    $page = $data['page'];
                }
                if (isset($data['name'])) {
                    $where[] = ['name','like', '%' . $data['name'] . '%'];
                }
                $list = CompanyModel::with(['optionsList'])->where($where)
                    ->paginate(15, false, ['page' => $page]);
            }

            $this->success('提交成功！',null,$list);
        }

        // 推荐用户注册会员卡得积分
        $user_card_register = Db::name('option')->where('option_name','user_card_register'.$company_id)->value('option_value');

        $this->assign('user_card_register', $user_card_register);

        // 推荐用户购买产品得积分比例
        $user_buy = Db::name('option')->where('option_name','user_buy'.$company_id)->value('option_value');

        $this->assign('user_buy', $user_buy);

        // 分享奖励说明 sharing_of_rewards
        $sharing_of_rewards = Db::name('option')->where('option_name','sharing_of_rewards'.$company_id)->value('option_value');

        $this->assign('sharing_of_rewards', $sharing_of_rewards);

        return $this->fetch();
    }

}
